package com.ld.config;

import com.ld.properties.HelloProperties;
import com.ld.service.HelloService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: Liudao
 * @Date: 2022-08-23-17:09
 * @Description:
 */
@Configuration//此注解有个属性proxyBeanMethods,默认是true，SpringBoot总会检查这个组件是否在容器中有。如果有就不再创建，否则，每次使用他创建新的bean
@EnableConfigurationProperties(HelloProperties.class)
@ConditionalOnClass(HelloService.class) //条件注解 判断路径下是否有此类
public class HelloAutoConfiguration {
    private final HelloProperties properties;

    public HelloAutoConfiguration(HelloProperties properties) {
        this.properties = properties;
    }

    @Bean
    public HelloService helloService(){
        HelloService helloService = new HelloService();
        helloService.setName(properties.getName());
        helloService.setHero(properties.getHero());
        return helloService;
    }
}
